package com.winit.schedule.core.utils;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

/**
 * 工具类
 * 
 * @author jianke.zhang 2015-4-2 下午2:46:20
 * @since 1.0
 */
public class NetUtils {

    /**
     * 获取本地IP地址
     * 
     * @return
     */
    public static String getLocalIP() {
        try {
            String hostAddress = InetAddress.getLocalHost().getHostAddress();

            if ("127.0.0.1".equals(hostAddress)) {
                try {
                    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                        NetworkInterface intf = en.nextElement();
                        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                            InetAddress inetAddress = enumIpAddr.nextElement();
                            if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()
                                && inetAddress.isSiteLocalAddress() && inetAddress instanceof Inet4Address) {
                                hostAddress = inetAddress.getHostAddress().toString();
                                if (!"127.0.0.1".equals(hostAddress)) {
                                    break;
                                }
                            }

                        }
                    }
                } catch (SocketException ex) {
                    return "";
                }
            }
            System.out.println(hostAddress);
            return hostAddress;

        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 获取本地主机名
     * 
     * @return
     */
    public static String getLocalHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (Exception e) {
            return "";
        }
    }

    /***
     * true:already in using false:not using
     * 
     * @param port
     */
    public static boolean isLoclePortUsing(int port) {
        boolean flag = true;
        try {
            flag = isPortUsing("127.0.0.1", port);
        } catch (Exception e) {
        }
        return flag;
    }

    /***
     * true:already in using false:not using
     * 
     * @param host
     * @param port
     * @throws UnknownHostException
     */
    public static boolean isPortUsing(String host, int port) throws UnknownHostException {
        boolean flag = false;
        InetAddress theAddress = InetAddress.getByName(host);
        Socket socket = null;
        try {
            socket = new Socket(theAddress, port);
            flag = true;
        } catch (IOException e) {

        } finally {
            if (null != socket) {
                try {
                    socket.close();
                } catch (IOException e) {
                }
            }
        }
        return flag;
    }

    public static void main(String[] args) {
        NetUtils.getLocalIP();

        System.out.println(NetUtils.isLoclePortUsing(8019));
        System.out.println(NetUtils.isLoclePortUsing(8019));
    }
}
